package com.skateboard.reelviewtest.rotate;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.PasswordTransformationMethod;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.animation.AnimatorSet;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.skateboard.reelviewtest.R;


/**
 * Created by <a href="http://blog.csdn.net/student9128">Kevin</a> on 2017/8/17.
 * <p>
 * <h3>Description:</h3>
 * <p/>
 * <p/>
 */
public class MyDialog extends Dialog {


    private Context context;

    RelativeLayout container;
    LinearLayout layout1;
    LinearLayout layout2;



    //接口回调传递参数
    private OnClickListenerInterface mListener;
//
    private String strContent;


    private int centerX;
    private int centerY;
    private int depthZ = 1;
    private int duration = 2000;
    private Animation rotateAnimation1;
    private Animation rotateAnimation2;
    private Animation rotateAnimation3;
    private Animation rotateAnimation4;
    private Animation scaleAnimation;
    AnimationSet setAnimation;

    private boolean isOpen = false;

    public interface OnClickListenerInterface {

        /**
         * 确认,
         */
        void doConfirm();

        /**
         * 取消
         */
        public void doCancel();
    }

    public MyDialog(Context context) {
        super(context);
        this.context = context;
    }

    public MyDialog(Context context, String content) {
        super(context);
        this.context = context;
        this.strContent = content;

    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            startAnimation();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉系统的黑色矩形边框
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.dialog_my, null);
        setContentView(view);
        container=findViewById(R.id.container);
        layout1=findViewById(R.id.layout_1);
        layout2=findViewById(R.id.layout_2);

        init();
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        handler.sendEmptyMessageDelayed(0,100);
        //startAnimation();
    }

    public void init() {
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.6
        lp.height = (int) (d.heightPixels * 1); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);
        //setCanceledOnTouchOutside(false);
        //setCancelable(true);


    }




    private void startAnimation() {
        centerX = container.getWidth() / 2;
        centerY = container.getHeight() / 2;
        if (setAnimation == null) {
            initAnim();
        }

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(rotateAnimation1);
        animationSet.addAnimation(scaleAnimation);

//        container.startAnimation(rotateAnimation1);
//        layout1.startAnimation(scaleAnimation);
//        layout2.startAnimation(scaleAnimation);


        container.startAnimation(animationSet);

    }
    private void startAnimation3() {
        centerX = container.getWidth() / 2;
        centerY = container.getHeight() / 2;
        if (setAnimation == null) {
            initAnim();
        }
        container.startAnimation(rotateAnimation1);

        ObjectAnimator scalAnimatorx=ObjectAnimator.ofFloat(container, "scaleX", 1f, 2f);
        ObjectAnimator scalAnimatory=ObjectAnimator.ofFloat(container, "scaleY", 1f, 2f);
        AnimatorSet set=new AnimatorSet();
        set.play(scalAnimatorx).with(scalAnimatory);
        set.setDuration(duration);
        set.start();


    }
    private void startAnimation2() {
        centerX = container.getWidth() / 2;
        centerY = container.getHeight() / 2;

        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        Rotate3dAnimation rotateAnimation1 = new Rotate3dAnimation(context,0, 360, centerX, centerY, depthZ, true);
        rotateAnimation1.setDuration(duration);
        rotateAnimation1.setFillAfter(true);
        rotateAnimation1.setInterpolator(new LinearInterpolator());

        ObjectAnimator scalAnimatorx=ObjectAnimator.ofFloat(container, "scaleX", 1f, 1.5f);
        ObjectAnimator scalAnimatory=ObjectAnimator.ofFloat(container, "scaleY", 1f, 1.5f);
        AnimatorSet set=new AnimatorSet();
        set.play(scalAnimatorx).with(scalAnimatory);
        set.setDuration(duration);
        set.start();
        container.startAnimation(rotateAnimation1);

    }

    /**
     * 卡牌文本介绍打开效果：注意旋转角度
     */
    private void initAnim() {
        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        rotateAnimation1 = new Rotate3dAnimation(context,0, 90, centerX, centerY, depthZ, true);
        rotateAnimation1.setDuration(duration);
        rotateAnimation1.setFillAfter(true);
        rotateAnimation1.setInterpolator(new LinearInterpolator());
        rotateAnimation1.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                layout1.setVisibility(View.INVISIBLE);
                layout2.setVisibility(View.VISIBLE);

                container.startAnimation(rotateAnimation2);
            }
        });

        //从270到360度，顺时针旋转视图，此时reverse参数为false，达到360度动画结束时视图变得可见
        rotateAnimation2 = new Rotate3dAnimation(context,90, 180, centerX, centerY, depthZ, false);
        rotateAnimation2.setDuration(duration);
        rotateAnimation2.setFillAfter(true);
        rotateAnimation2.setInterpolator(new DecelerateInterpolator());
        rotateAnimation2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
//                layout1.setVisibility(View.VISIBLE);
//                layout2.setVisibility(View.INVISIBLE);

                container.startAnimation(rotateAnimation3);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        //scaleAnimation=new ScaleAnimation(1.f, 1.0f, 1f, 1.0f);
        scaleAnimation = new ScaleAnimation(0.5f, 1, 0.5f, 1,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(4000);
        scaleAnimation.setFillAfter(true);
        scaleAnimation.setRepeatCount(0);

        rotateAnimation3 = new Rotate3dAnimation(context,180, 270, centerX, centerY, depthZ, true);
        rotateAnimation3.setDuration(duration);
        rotateAnimation3.setFillAfter(true);
        rotateAnimation3.setInterpolator(new AccelerateInterpolator());
        rotateAnimation3.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                layout1.setVisibility(View.VISIBLE);
                layout2.setVisibility(View.INVISIBLE);
                container.startAnimation(rotateAnimation4);
            }
        });

        rotateAnimation4 = new Rotate3dAnimation(context,270, 360, centerX, centerY, depthZ, false);
        rotateAnimation4.setDuration(duration);
        rotateAnimation4.setFillAfter(true);
        rotateAnimation4.setInterpolator(new DecelerateInterpolator());
        container.startAnimation(rotateAnimation4);
    }

}